package com.fakesmoker.aidawnda.scoring;/**
 * @program: aidawnda-backend
 * @author: lcy
 * @create: 2024-11-09 14:46
 */

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fakesmoker.aidawnda.model.dto.question.QuestionContentDto;
import com.fakesmoker.aidawnda.model.entity.App;
import com.fakesmoker.aidawnda.model.entity.Question;
import com.fakesmoker.aidawnda.model.entity.ScoringResult;
import com.fakesmoker.aidawnda.model.entity.UserAnswer;
import com.fakesmoker.aidawnda.model.enums.AppScoringStrategy;
import com.fakesmoker.aidawnda.model.enums.AppTypeEnum;
import com.fakesmoker.aidawnda.service.AppService;
import com.fakesmoker.aidawnda.service.QuestionService;
import com.fakesmoker.aidawnda.service.ScoringResultService;
import com.fakesmoker.aidawnda.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 2024/11/9,
 */

/**
 * 自定义评分策略实现类
 * @author: lcy
 */
@ScoringStrategyConfigAnnotation(appType = 1,scoringStrategy =0)
public class CustomEvaluateScoringStrategyImpl implements ScoringStrategy {
    @Resource
    private ScoringResultService scoringResultService;
    @Resource
    private QuestionService questionService;



    @Override
    public UserAnswer doScore(List<String> choices, App app) {
        //根据id查询到题目和题目结果信息
        Long appId = app.getId();
        Question question=questionService.getOne(Wrappers.lambdaQuery(Question.class)
                .eq(Question::getAppId, appId));
        List<ScoringResult> scoringResultList=scoringResultService.list(Wrappers.lambdaQuery(ScoringResult.class)
                .eq(ScoringResult::getAppId, appId));

        // 解析题目内容，获取每个选项对应的属性
        String questionContent = question.getQuestionContent();
        List<QuestionContentDto> questionContentDtoList = JSONUtil.toList(questionContent,QuestionContentDto.class);

        // 统计用户每个选择对应的属性个数，如I=10，E=5
        Map<String, Integer> propertyCountMap = new HashMap<>();
        for (int i = 0; i < choices.size(); i++) {
            String choice = choices.get(i);
            for (QuestionContentDto.Option option : questionContentDtoList.get(i).getOptions()) {
                if (option.getKey().equalsIgnoreCase(choice)) {
                    String result = option.getResult();
                    propertyCountMap.put(result, propertyCountMap.getOrDefault(result, 0) + 1);
                }
            }
        }

        // 遍历每种评分结果，计算哪个结果得分更高
        ScoringResult highestScoringResult = null;
        int highestScore = Integer.MIN_VALUE;
        for (ScoringResult scoringResult : scoringResultList) {
            String resultProp = scoringResult.getResultProp();
            List<String> resultPropList = JSONUtil.toList(resultProp,String.class);
            int score = calculateScore(resultPropList, propertyCountMap);
            if (score > highestScore) {
                highestScore = score;
                highestScoringResult = scoringResult;
            }
        }

        // 构造返回值，填充答案对象的属性
        UserAnswer userAnswer = new UserAnswer();

        userAnswer.setAppId(appId);
        userAnswer.setAppType(app.getAppType());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setResultScore(highestScore);

        if (highestScoringResult!= null) {
            userAnswer.setResultId(highestScoringResult.getId());
            userAnswer.setResultName(highestScoringResult.getResultName());
            userAnswer.setResultDesc(highestScoringResult.getResultDesc());
            userAnswer.setResultPicture(highestScoringResult.getResultPicture());
        }

        return userAnswer;
    }

    @Override
    public UserAnswer doScoreBySingle(List<String> choices, App app) throws InterruptedException {
        return null;
    }

    // 计算某个评分结果的得分的方法
    private int calculateScore(List<String> resultPropList, Map<String, Integer> propertyCountMap) {
        int score = 0;
        for (String prop : resultPropList) {
            score += propertyCountMap.getOrDefault(prop, 0);
        }
        return score;
    }
}
